<template>
    <div class="login_container">
        <div class="login_box">
            <div class="avatar_box">
                <img src="../assets/logozbd.png"/>
            </div>
            <!--登录-->
            <el-form ref="loginFormRef" v-model="loginForm"  label-width="0" class="login_form">
                <el-form-item prop="username">
                    <el-input v-model="loginForm.username" prefix-icon="el-icon-user-solid" @keyup.enter.native="handleLogin" placeholder="请输入用户名"></el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input v-model="loginForm.password"  @keyup.enter.native="handleLogin" prefix-icon="el-icon-more" placeholder="请输入用户密码" type="password"></el-input>
                </el-form-item>
                <el-form-item class="btns">
                    <el-button type="primary" @click="handleLogin">登录</el-button>
                    <el-button type="info" @click="resetLoginForm">重置</el-button>
                </el-form-item>
            </el-form>


        </div>
    </div>
</template>

<script>
    import {Message} from "element-ui";

    export default {
        name: "Login",
        data() {
            return {
                loginForm: {
                    username: '',
                    password: ''
                },
                system_config: {}
            }
        },
        created() {

        },
        methods: {
            handleLogin() {

                if (!this.loginForm.username){
                    return Message.error("用户名不能为空")
                }
                if (!this.loginForm.password){
                    return Message.error("密码不能为空")
                }

                this.$http.post('login', this.loginForm).then(res => {
                    if (res.data.meta.status == 200) {
                        Message.success(res.data.meta.msg)
                        let token = res.data.data.token
                        this.$store.commit('saveUsername', res.data.data.username)
                        this.$store.commit('saveUserCname', res.data.data.cname)
                        window.sessionStorage.setItem('token', token)
                        window.sessionStorage.setItem('username', res.data.data.username)
                        window.sessionStorage.setItem('cname', res.data.data.cname)
                        this.$router.push('/home')

                    }
                    if (res.data.meta.status != 200) {
                        Message.error(res.data.meta.msg)
                    }
                })


            },
            resetLoginForm() {
                // console.log(this)
                this.$refs.loginFormRef.resetFields()
                this.loginForm.username = this.loginForm.password = ''
            },
        }
    }
</script>

<style scoped lang="less">

    .login_container {
        background-color: #2b4b6b;
        height: 100%;
    }

    .login_box {
        width: 450px;
        height: 300px;
        background-color: #fff;
        border-radius: 3px;
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);

        .avatar_box {
            height: 130px;
            width: 130px;
            border: 1px solid #eee;
            border-radius: 50%;
            padding: 10px;
            box-shadow: 0 0 10px #ddd;
            position: absolute;
            left: 50%;
            transform: translate(-50%, -50%);
            background-color: #fff;

            img {
                background-color: #eee;
                width: 100%;
                height: 100%;
                border-radius: 50%;
            }
        }
    }

    .btns {
        display: flex;
        justify-content: flex-end;
    }

    .login_form {
        position: absolute;
        bottom: 0;
        width: 100%;
        padding: 0 20px;
        box-sizing: border-box;


    }


</style>
